using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Adge.Models;
using NHibernate.Expression;
using Adge.Common;
using Adge.BLL;
using Adge.Common.Collections;


namespace Adge.Web.UI
{
    public partial class catalog : AdgePageBase
    {
        private string ObjCatname
        {
            get
            {
                string catname = string.Empty;

                if (!string.IsNullOrEmpty(Request["catname"]))
                {
                    catname = Request["catname"];
                }

                return catname;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPicCat();
                if (ddlPicCat.SelectedIndex > -1)
                {
                    BindPicByCat(ddlPicCat.SelectedValue);
                }
            }
        }

        private void BindPicByCat(string catid)
        {
            Adge.BLL.AdgeArBase<AttachmentInfo> attlist = new Adge.BLL.AdgeArBase<AttachmentInfo>();
            Adge.BLL.AdgeArBase<CategoryInfo> catlist = new Adge.BLL.AdgeArBase<CategoryInfo>();
            CategoryInfo catinfo = catlist.GetEntityById(int.Parse(catid));

            #region Bind Catgory
            ICriterion[] criterias = new ICriterion[] { new EqExpression("AdgeInfoCategory", catinfo) };
            Order[] orders = new Order[] { Order.Desc("AdgeInfoCategory") };
            AttachmentInfo[] entities = attlist.SlicedFindAll(0, AdgeConfig.MaxRecordNumber, orders, criterias);
            repFiles.DataSource = entities;
            repFiles.DataBind();
            #endregion
        }

        private void InitPicCat()
        {
            string catname = AdgeConfig.GetCatNameByIndex(ObjCatname).Name;
            //lblCatame.Text = catname;
            AdgeArBase<CategoryInfo> bllSub = new AdgeArBase<CategoryInfo>();
            Order[] orders = new Order[] { };
            ICriterion[] parentCriterias = new ICriterion[] { new EqExpression("Catname", catname) };//, new EqExpression("Visible", true)
            CategoryInfo[] parentSub = bllSub.SlicedFindAll(0, AdgeConfig.MaxRecordNumber, orders, parentCriterias);
            IList<CategoryInfo> subs = new List<CategoryInfo>();
            if (parentSub.Length > 0 && parentSub[0].Children.Count > 0)
            {
                foreach (CategoryInfo var in parentSub[0].Children)
                {
                    if (var.Visible == true)
                    {
                        subs.Add(var);
                    }
                }
            }
            else
            {
                subs = Adapters.ArrayToList<CategoryInfo>(parentSub);
            }
            ddlPicCat.DataSource = subs;
            ddlPicCat.DataTextField = "Catname";
            ddlPicCat.DataValueField = "CatId";
            ddlPicCat.DataBind();

        }

        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            //reset index
            repFiles.CurrentPageIndex = e.NewPageIndex;

            if (ddlPicCat.SelectedIndex > -1)
            {
                BindPicByCat(ddlPicCat.SelectedValue);
            }
        }

        protected string GetImageUrl(string attach)
        {
            string url = AdgeConfig.FileSoragePath + attach;
            
            return this.ResolveUrl(url);
        }

        protected void ddlPicCat_SelectedIndexChanged(object sender, EventArgs e)
        {
            repFiles.CurrentPageIndex = 0;
            if (ddlPicCat.SelectedIndex > -1)
            {
                BindPicByCat(ddlPicCat.SelectedValue);
            }
        }
}
}
